import 'package:flutter/material.dart';
import 'package:xiongan_app_yy/common/user_singleton.dart';
import 'package:xiongan_app_yy/net/internet_api.dart';
import 'package:xiongan_app_yy/pages/common/LoadBuilder.dart';
import 'package:xiongan_app_yy/widgets/comm/left_icon_widget.dart';

//执行抽样
class ExecutiveSampling extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _ExecutiveSampling();
  }
}

//ListWidget
class _ExecutiveSampling extends State<ExecutiveSampling> {
  List<dynamic> _data = List();
  bool load=true;

  @override
  void initState() {
    super.initState();
    getData();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        leading: LeftIconWidget(icon: Icons.chevron_left, isize: 36),
        backgroundColor: Color(0XFF00A5F1),
        title: Text(
          "执行抽样",
          style: TextStyle(fontSize: 18),
        ),
        actions: <Widget>[
          new IconButton(
            // action button
            padding: EdgeInsets.only(),
            iconSize: 22,
            icon: new Icon(IconData(0xe617, fontFamily: "wuIconFont")),
            onPressed: () {
              Navigator.pushNamed(context, "/executivesamplingSearchPage");
            },
          )
        ],
        elevation: 0,
        centerTitle: true,
      ),
      body: LoadBuilder(visibility: load,emptyVisibility: !load&&_data.length==0,
          builder: RefreshIndicator(child:  ListView.separated(itemBuilder: (BuildContext context, int index) {
            return TextCartItem(
              data: _data,
              index: index,
              onTapFuns: (int index) {
                Navigator.of(context)
                    .pushNamed("/executivesampling_list",arguments: _data[index]["id"]).then((data){
                  if(data=="back"){
                    setState(() {
                      load=true;
                    });
                    getData();
                  }
                });
              },
            );
          },
              separatorBuilder: (BuildContext context, int index) {
                return Divider(color: Colors.black26);
              },
              itemCount: _data.length),onRefresh: _refresh,)

      ),
    );
  }

  Future <void> _refresh()  async{
    await Future.delayed(Duration(seconds: 1),(){
      return null;
    });
    setState(() {
      load=true;
    });
    getData();
  }

  void getData() {
    InternetAPI.getInstance().get(
      "WTesting/DescSpotcheckTask/GetAllDescSpotcheckTask",
          (data) {
        print(data.toString());
        setState(() {
          load=false;
          _data=data;
        });
      },
      params: {
        "operaterUserName": DataSingleton().Data.username,
        "operaterUserId": DataSingleton().Data.id,
        "tenantId": DataSingleton().Data.tenantId,
        "pageno": 1,
        "pageSize": -1,
      },
    );
  }
}

class _Text extends Text {
  TextStyle style;
  _Text(String data,
      {this.style = const TextStyle(color: Color(0xff646464), fontSize: 14)})
      : super(data, style: style);
}

class TextCartItem extends StatelessWidget {
  final List<dynamic> data;
  final int index;
  final Function(int index) onTapFuns;

  TextCartItem(
      {Key key, @required this.data, @required this.index, this.onTapFuns})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    Color textColor =
    data[index]['ispickup'] == true ? Color(0xFF2FC480) : Color(0xFF00A5F1);
    return GestureDetector(
      child: Container(
          padding: EdgeInsets.all(10.0),
          decoration: BoxDecoration(
              //解决GestureDetector Widget事件触发面问题
              border: Border.all(color: Colors.transparent, width: 1)),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              _Text("检测任务号:${data[index]['taskno']}"),
              Container(
                  padding: EdgeInsets.only(top: 10, bottom: 10),
                  child: RichText(
                    text: TextSpan(
                      text: '抽样状态:',
                      style:
                      TextStyle(fontSize: 14.0, color: Color(0xff646464)),
                      children: <TextSpan>[
                        TextSpan(
                          text: data[index]['taskstate'],
                          style: TextStyle(fontSize: 14.0, color: textColor),
                        ),
                      ],
                    ),
                  )),
              _Text("送检时间:${data[index]['creationTime']}")
            ],
          )),
      onTap: () {
        if (onTapFuns != null) {
          onTapFuns(index);
        }
      },
    );
  }
}
